﻿using System;
using COM.DTO;
using NHibernate;
using COM.Logging;
using NHibernate.Criterion;
using System.Collections.Generic;

namespace DAL.DAO
{
    class CommentDAO
    {
        public static void Add(Comment comment)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.SaveOrUpdate(comment);
                    transaction.Commit();
                }
            }
        }
        public static void Update(Comment comment)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Update(comment);
                    transaction.Commit();
                }
            }
        }

        public static void Remove(Comment comment)
        {
            {
                using (ISession session = NHibernateHelper.OpenSession())
                using (ITransaction transaction = session.BeginTransaction())
                {
                    session.Delete(comment);
                    transaction.Commit();
                }
            }
        }

        public static Comment GetCommentById(String commentID)
        {
            using (ISession session = NHibernateHelper.OpenSession())
                return session.Get<Comment>(commentID);
        }
        public static List<Comment> GetCommentByHotelID(string hotelID)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var commentList = session
                    .CreateCriteria(typeof(List<Comment>))
                    .Add(Restrictions.Eq("HotelId", hotelID))
                    .UniqueResult<List<Comment>>();
                return commentList;
            }

        }
        public static List<Comment> GetCommentByUserID(string userId)
        {
            using (ISession session = NHibernateHelper.OpenSession())
            {
                var commentList = session
                    .CreateCriteria(typeof(List<Comment>))
                    .Add(Restrictions.Eq("UserId", userId))
                    .UniqueResult<List<Comment>>();
                return commentList;
            }

        }
    }
}
